package com.hangzhou.welbeing.welbeinginstrument.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.hangzhou.welbeing.welbeinginstrument.R;
import com.hangzhou.welbeing.welbeinginstrument.bean.GuessYouLikeDateBean;
import com.hangzhou.welbeing.welbeinginstrument.config.RBConstants;
import com.hangzhou.welbeing.welbeinginstrument.inteface.OnRecyclerViewItemClickListener;
import com.hangzhou.welbeing.welbeinginstrument.tool.GlideCircleTransform;

import java.util.ArrayList;
import java.util.List;

/**
 * 时间 2017 年 09 月 07.
 * 作者 Administrator  刘传华
 * 公司 杭州惟君医疗科技有限公司
 * 包名 com.hangzhou.welbeing.pregnant.welbeing_pregnant.adapter
 * 工程名 welbeing_pregnant
 */
public class SmallModuleAdapter  extends  RecyclerView.Adapter<SmallModuleAdapter.ViewHolder> implements View.OnClickListener{
    private static final String TAG = "SmallModuleAdapter";
    private OnRecyclerViewItemClickListener mOnItemClickListener = null;
    private List<GuessYouLikeDateBean.DataBean> mlist = new ArrayList<>();
    private  Context mcontext;


    LayoutInflater mInflater;
    private static final int TYPE_ITEM   = 0;
    private static final int TYPE_FOOTER = 1;

    //上拉加载更多
    public static final int PULLUP_LOAD_MORE = 0;
    //正在加载中
    public static final int LOADING_MORE     = 1;
    //没有加载更多 隐藏
    public static final int NO_LOAD_MORE     = 2;

    //上拉加载更多状态-默认为0
    private int mLoadMoreStatus = 0;

    public SmallModuleAdapter(List<GuessYouLikeDateBean.DataBean> bean, Context context) {
        this.mcontext = context;
       // mInflater = LayoutInflater.from(context);
        mlist.addAll(bean);
    }

    public void setOnItemClickListener(OnRecyclerViewItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
       /* if(viewType == TYPE_FOOTER){
            View itemView = mInflater.inflate(R.layout.load_more_footview_layout, parent, false);
            return new FooterViewHolder(itemView);
        }else if(viewType == TYPE_ITEM){*/
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_small_module_recyclerview,parent,false);
            ViewHolder viewHolder = new ViewHolder(view);
            view.setOnClickListener(this);
            return viewHolder;

    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
      /*  if(holder instanceof FooterViewHolder){
            FooterViewHolder footerViewHolder  = (SmallModuleAdapter.FooterViewHolder) holder;
            switch (mLoadMoreStatus) {
               *//* case PULLUP_LOAD_MORE:
                    footerViewHolder.mTvLoadText.setText("上拉加载更多...");
                    break;*//*
                case LOADING_MORE:
                    footerViewHolder.tvLoadText.setText("正加载更多...");
                    break;
                case NO_LOAD_MORE:
                    //隐藏加载更多
                    footerViewHolder.rl_01.setVisibility(View.GONE);
                    break;
            }

        }else*/
          //  ViewHolder viewHolder = (ViewHolder) holder;
            Glide.with(mcontext).load(RBConstants.SERVER_URL+"/Public/Uploads/"+mlist.get(position).cus_logo).transform(new GlideCircleTransform(mcontext)).into(holder.iv_personal_head);
        holder.tv_name.setText(mlist.get(position).cus_name);
        holder.tv_date.setText(mlist.get(position).addtime);
            int state =  mlist.get(position).state; //后台没改变
            if(state == 0){
                holder.tv_attention.setText("关注");
            }else if(state == 1){
                holder.tv_attention.setText("已关注");
            }
//                   String po_url =  mlist.get(position).po_url;
//            if(po_url.equals("") || po_url == null){
//                holder.ll_user_picture.setVisibility(View.GONE);
//            }
//            String[] arr =  po_url.split(",");
//            if(arr.length == 1){
//                Glide.with(mcontext).load(RBConstants.SERVER_URL+"/Public/Uploads/"+po_url).into(holder.iv_user_picture_01);
//                holder.iv_user_picture_02.setVisibility(View.GONE);
//                holder.iv_user_picture_03.setVisibility(View.GONE);
//
//            }else if(arr.length == 2){
//                Glide.with(mcontext).load(RBConstants.SERVER_URL+"/Public/Uploads/"+arr[0]).into(holder.iv_user_picture_01);
//                Glide.with(mcontext).load(RBConstants.SERVER_URL+"/Public/Uploads/"+arr[1]).into(holder.iv_user_picture_02);
//                holder.iv_user_picture_03.setVisibility(View.INVISIBLE);
//
//
//            }else {
//                Glide.with(mcontext).load(RBConstants.SERVER_URL+"/Public/Uploads/"+arr[0]).into(holder.iv_user_picture_01);
//                Glide.with(mcontext).load(RBConstants.SERVER_URL+"/Public/Uploads/"+arr[1]).into(holder.iv_user_picture_02);
//                Glide.with(mcontext).load(RBConstants.SERVER_URL+"/Public/Uploads/"+arr[2]).into(holder.iv_user_picture_03);
//
//            }

        holder.tv_attention.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(mOnItemClickListener != null ){
                        mOnItemClickListener.onItemFirstButtonClick(v,position);
                    }
                }
            });
        holder.tv_titile.setText(mlist.get(position).po_name);
        holder.tv_content.setText(mlist.get(position).content);
        holder.tv_commentaries.setText(mlist.get(position).count);
        holder.tv_thumb_up.setText(mlist.get(position).hits_like);



    }
   /* @Override
    public int getItemViewType(int position) {

        if (position + 1 == getItemCount()) {
            //最后一个item设置为footerView
            return TYPE_FOOTER;
        } else {
            return TYPE_ITEM;
        }
    }*/

   /* *//**
     * 更新加载更多状态
     * @param
     *//*
    public void changeMoreStatus(int status){
        mLoadMoreStatus=status;
        notifyDataSetChanged();
    }*/
    @Override
    public int getItemCount() {
        return mlist.size();
    }

    @Override
    public void onClick(View view) {
        if(mOnItemClickListener != null ){
            mOnItemClickListener.onItemClick(view);
        }
    }
    /**
     * 身体布局Holder
     */
    static class ViewHolder extends RecyclerView.ViewHolder {
        private TextView tv_name;
        private  TextView tv_date;
        private  TextView tv_attention;
        private  TextView tv_titile;
        private  TextView tv_content;
        private  TextView tv_thumb_up;
        private  TextView tv_commentaries;
        private ImageView iv_personal_head;

        private ImageView iv_user_picture_01;
        private ImageView iv_user_picture_02;
        private ImageView iv_user_picture_03;
        private LinearLayout ll_user_picture;
        public ViewHolder(View itemView) {
            super(itemView);
            tv_name =  (TextView) itemView.findViewById(R.id.item_small_module_recyclerview_tv_name);
            tv_date =  (TextView) itemView.findViewById(R.id.item_small_module_recyclerview_tv_date);
            tv_titile =  (TextView) itemView.findViewById(R.id.item_small_module_recyclerview_tv_titile);
            tv_attention =  (TextView) itemView.findViewById(R.id.item_small_module_recyclerview_tv_attention);
            tv_content =  (TextView) itemView.findViewById(R.id.item_small_module_recyclerview_tv_content);
            tv_thumb_up =  (TextView) itemView.findViewById(R.id.item_small_module_recyclerview_tv_thumb_up);
            tv_commentaries =  (TextView) itemView.findViewById(R.id.item_small_module_recyclerview_tv_commentaries);
            iv_personal_head =  (ImageView) itemView.findViewById(R.id.item_small_module_recyclerview_iv_personal_head);

            iv_user_picture_01 =  (ImageView) itemView.findViewById(R.id.item_small_module_recyclerview_iv_user_picture_01);
            iv_user_picture_02 =  (ImageView) itemView.findViewById(R.id.item_small_module_recyclerview_iv_user_picture_02);
            iv_user_picture_03 =  (ImageView) itemView.findViewById(R.id.item_small_module_recyclerview_iv_user_picture_03);
            ll_user_picture =  (LinearLayout) itemView.findViewById(R.id.item_small_module_recyclerview_ll_user_picture);
        }
    }

  /*  public class FooterViewHolder extends RecyclerView.ViewHolder {
        private ProgressBar pbLoad;
        private TextView tvLoadText;
        private RelativeLayout rl_01;
        public FooterViewHolder(View itemView) {
            super(itemView);
            pbLoad =  (ProgressBar) itemView.findViewById(R.id.pbLoad);
            tvLoadText =  (TextView) itemView.findViewById(R.id.tvLoadText);
            rl_01 =  (RelativeLayout) itemView.findViewById(R.id.load_more_footview_layout_rl_01);
        }
    }*/
}
